package com.jingzhi.count.netty;

import com.jingzhi.count.utils.HexDecoder;
import com.jingzhi.count.utils.HexEncoder;
import io.netty.channel.Channel;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.handler.codec.LengthFieldBasedFrameDecoder;
import io.netty.handler.codec.LengthFieldPrepender;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;
import io.netty.handler.timeout.IdleStateHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

/**
 * 作者： 张恒同
 * 时间：2018/3/23   15:12
 * 描述：
 */
@Component
public class CountServerInitializer extends ChannelInitializer<Channel>{

    @Autowired
    private HttpServerRevHandler serverRevHandler;

    public CountServerInitializer() {

    }

    @Override
    protected void initChannel(Channel ch) throws Exception {
        ChannelPipeline pipeline = ch.pipeline();
        //处理日志
        pipeline.addLast(new LoggingHandler(LogLevel.INFO));

        //处理心跳
        pipeline.addLast(new IdleStateHandler(0, 0, 1800, TimeUnit.SECONDS));

        pipeline.addLast("decoder",new HexDecoder());//HexDecoder
        pipeline.addLast("encoder",new HexEncoder());
        pipeline.addLast(serverRevHandler);
    }
}
